//
//  JWFindFreightVC.m
//  jinwang
//
//  Created by jzb on 2018/4/12.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWFindFreightVC.h"
#import "JWFBFacadeVCModel.h"
#import "JWFBFacadeTagVC.h"
#import "JWFindBusinessAddTagVC.h"
#import "JWFindFreightReleaseVC.h"
#import "JWFindFreightShopRuZhuVC.h"

@interface JWFindFreightVC ()
{
    
    TYAlertController *_alertController;
    TYAlertController *_SZalertController;
}
//定位
@property (nonatomic,strong) UIButton *addressBtn;
//发布
@property (nonatomic,strong) UIButton *fabuBtn,*RuZhuBuBtn;
@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;
@property (nonatomic,strong) UIView *BtnView,*OrderZTView,*shezhiBGView;
@property (nonatomic, strong) NSMutableArray *ListmodelArr,*list;

@end

@implementation JWFindFreightVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self makeUrl_Freight_freightShopStatus];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"拼车运货";
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back_common"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    UIBarButtonItem *photoItem = [[UIBarButtonItem alloc]initWithCustomView:self.addressBtn];
    
    self.navigationItem.leftBarButtonItems = @[backItem,photoItem];
    
    UIBarButtonItem *shaixuanItem = [[UIBarButtonItem alloc]initWithCustomView:self.fabuBtn];
    self.navigationItem.rightBarButtonItem = shaixuanItem;
    
    [self makeUrl_Freight_defaultTag];
}

- (void)segHeadViewBuild{
    
    _list = [NSMutableArray new];
    
    for (int i = 0; i < _ListmodelArr.count; i++) {
        
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        
        [_list addObject:model.tag_name];
    }
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-60, 44) titles:_list headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutLeft];
    //    _segHead.fontScale = 1.1;
    _segHead.fontSize = 17;
    _segHead.deSelectColor = kRGBColor(80, 80, 80);
    _segHead.selectColor = UIColorHex(0xEEA72B);
    _segHead.lineScale = .8;
    _segHead.bottomLineHeight = .5;
    _segHead.lineColor = UIColorHex(0xEEA72B);
    _segHead.bottomLineColor = kRGBColor(230, 230, 230);
    //    _segHead.moreButton_width = 60;
    
    UIButton *moreBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-45, 4, 30, 30)];
    [moreBtn setImage:[UIImage imageNamed:@"jw_find_shop_blackAddIcom"] forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(moreBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:moreBtn];
    UIView *lineVeiw = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth - 60, 43.5, 60, .5)];
    lineVeiw.backgroundColor = kRGBColor(230, 230, 230);
    [self.view addSubview:lineVeiw];
    
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight-44-50-64) vcOrViews:[self vcArr:_list.count]];
    
    _segScroll.loadAll = YES;
    _segScroll.showIndex = 0;
    
    _segScroll.backgroundColor = [UIColor purpleColor];
    
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        
        [self.view addSubview:_segHead];
        [self.view addSubview:_segScroll];
    }];
    
    _BtnView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight-50-64, kScreenWidth, 50)];
    _BtnView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:_BtnView];
    
    UIButton *orderZTBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/2, 50)];
    orderZTBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    orderZTBtn.backgroundColor = kRGBColor(255, 135, 23);
    [orderZTBtn setTitle:@"订单状态" forState:UIControlStateNormal];
    orderZTBtn.tag = 2001;
    [orderZTBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [orderZTBtn addTarget:self action:@selector(MyFaBuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [_BtnView addSubview:orderZTBtn];
    
    UIButton *shezhiBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth/2, 0, kScreenWidth/2, 50)];
    shezhiBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    shezhiBtn.backgroundColor = kRGBColor(226, 119, 22);
    shezhiBtn.tag = 2002;
    [shezhiBtn setTitle:@"设置" forState:UIControlStateNormal];
    [shezhiBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shezhiBtn addTarget:self action:@selector(MyFaBuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [_BtnView addSubview:shezhiBtn];
    
    _RuZhuBuBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kScreenHeight-50-64, kScreenWidth, 50)];
    _RuZhuBuBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    _RuZhuBuBtn.tag = 2000;
    [self.view addSubview:_RuZhuBuBtn];
    CGFloat butHeight = 40;
    NSArray *OrderZTArr = @[@"正在接单",@"暂停接单",@"已经接单"];
    _OrderZTView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, butHeight*OrderZTArr.count)];
    _OrderZTView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < OrderZTArr.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, butHeight*i, _OrderZTView.width, butHeight)];
        [button setTitle:OrderZTArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_OrderZTView addSubview:button];
    }
    _OrderZTView.layer.cornerRadius = 5.0;
    _OrderZTView.layer.masksToBounds = YES;
    _alertController = [TYAlertController alertControllerWithAlertView:_OrderZTView preferredStyle:TYAlertControllerStyleAlert];
    _alertController.backgoundTapDismissEnable = YES;
    
    NSArray *shezhiArr = @[@"固定位置接单",@"移动位置接单",@"订单交易记录",@"我的货运"];
    _shezhiBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, butHeight*shezhiArr.count)];
    _shezhiBGView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < shezhiArr.count; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, butHeight*i, _shezhiBGView.width, butHeight)];
        [button setTitle:shezhiArr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_shezhiBGView addSubview:button];
    }
    _shezhiBGView.layer.cornerRadius = 5.0;
    _shezhiBGView.layer.masksToBounds = YES;
    _SZalertController = [TYAlertController alertControllerWithAlertView:_shezhiBGView preferredStyle:TYAlertControllerStyleAlert];
    _SZalertController.backgoundTapDismissEnable = YES;
}
#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < _ListmodelArr.count; i++) {
        JWFBFacadeVCModel *model = _ListmodelArr[i];
        JWFBFacadeTagVC *FacadeTagVC = [[JWFBFacadeTagVC alloc]init];
        FacadeTagVC.tag_id = [model.tag_id stringValue];
        //        FacadeTagVC.parent_id = _parent_id;
        
        [arr addObject:FacadeTagVC];
    }
    return arr;
}
- (void)moreBtnOnclick:(UIButton *)sender{
    
    JWFindBusinessAddTagVC *vc = [JWFindBusinessAddTagVC new];
    vc.title = @"拼车运货";
    vc.ListmodelArr = _ListmodelArr;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)MyFaBuBtnOnclick:(UIButton *)sender{
    
    if (sender.tag == 2000) {
        
        JWFindFreightShopRuZhuVC *VC = [JWFindFreightShopRuZhuVC new];
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 2001){
        
        [self presentViewController:_alertController animated:YES completion:nil];
        
    }else if (sender.tag == 2002){
       
        [self presentViewController:_SZalertController animated:YES completion:nil];
        
    }
}
#pragma mark -网络请求
//获取默认标签
-(void)makeUrl_Freight_defaultTag{
    [[DDNetworkManagerDate makeUrl_Freight_defaultTagWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            _ListmodelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWFBFacadeVCModel *model = [JWFBFacadeVCModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            self.ListmodelArr = arr.mutableCopy;
            
            if (_ListmodelArr.count > 0) {
                
                [self segHeadViewBuild];
            }
            
        }
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//获取默认标签
-(void)makeUrl_Freight_freightShopStatus{
    
    [[DDNetworkManagerDate makeUrl_Freight_freightShopStatusWithtoken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            
            NSNumber *shop_status = [data objectForKey:@"shop_status"];
            //0申请中1通过2拒绝 3未申请
            if ([shop_status integerValue] == 0) {
                _RuZhuBuBtn.hidden = NO;
                [_RuZhuBuBtn setTitle:@"审核中" forState:UIControlStateNormal];
                _RuZhuBuBtn.backgroundColor = [UIColor lightGrayColor];
                _RuZhuBuBtn.userInteractionEnabled = NO;
                
            }else if ([shop_status integerValue] == 1){
                
                _RuZhuBuBtn.hidden = YES;
                _BtnView.hidden = NO;
                
            }else if ([shop_status integerValue] == 2){
                _RuZhuBuBtn.hidden = NO;
                _RuZhuBuBtn.backgroundColor = UIColorHex(0xEEA72B);
                [_RuZhuBuBtn setTitle:@"被拒绝，重新提交" forState:UIControlStateNormal];
                [_RuZhuBuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [_RuZhuBuBtn addTarget:self action:@selector(MyFaBuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
                
            }else if ([shop_status integerValue] == 3){
                _RuZhuBuBtn.hidden = NO;
                _RuZhuBuBtn.backgroundColor = UIColorHex(0xEEA72B);
                [_RuZhuBuBtn setTitle:@"商家入驻" forState:UIControlStateNormal];
                [_RuZhuBuBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [_RuZhuBuBtn addTarget:self action:@selector(MyFaBuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
}

#pragma mark -懒加载
- (UIButton *)addressBtn{
    if (!_addressBtn) {
        _addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addressBtn.frame = CGRectMake(0, 0, 44, 44);
        [_addressBtn setImage:[UIImage imageNamed:@"jw_find_dingwei"] forState:UIControlStateNormal];
        [_addressBtn addTarget:self action:@selector(addressBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressBtn;
}
#pragma mark -懒加载
- (UIButton *)fabuBtn{
    if (!_fabuBtn) {
        _fabuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fabuBtn.frame = CGRectMake(0, 0, 44, 44);
        [_fabuBtn setTitle:@"发布" forState:UIControlStateNormal];
        [_fabuBtn addTarget:self action:@selector(fabuBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fabuBtn;
}
- (void)fabuBtnOnclick:(UIButton *)sender{
    JWFindFreightReleaseVC *VC = [JWFindFreightReleaseVC new];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)addressBtnOnclick:(UIButton *)sender{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
